package com.molichuxing.services.infrastructure.service.impl;

import com.alibaba.fastjson.JSON;
import com.molichuxing.services.business.dto.request.create.GoodsSubscribePackageCreateDto;
import com.molichuxing.services.infrastructure.bean.GoodsSubscribeMileageBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.GoodsSubscribeMileageMapper;
import com.molichuxing.services.infrastructure.dto.response.GoodsSubscribeMileageDto;
import com.molichuxing.services.infrastructure.service.GoodsSubscribeMileageService;
import com.molichuxing.services.property.GoodsMileageTypeEnum;
import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 订阅里程包表 服务实现类
 * </p>
 *
 * @author xuelin yi
 * @since 2019-08-12
 */
@Service("goodsProductSubscribeMileageService")
public class GoodsSubscribeMileageServiceImpl implements GoodsSubscribeMileageService {

    private static final Logger log = Logger.getLogger(GoodsSubscribeServiceImpl.class);

    @Resource
    private GoodsSubscribeMileageMapper goodsSubscribeMileageMapper;

    @Override
    public Map<Integer, List<GoodsSubscribeMileageDto>> getGoodsSubscribeMileageById(List<Integer> goodsId) {
        log.info("根据商品id集合查询订阅里程包信息。入参 goodsId ： ["+ goodsId +"]");
        List<GoodsSubscribeMileageBean> result = goodsSubscribeMileageMapper.selectSubscribeMileageList(goodsId);
        log.info("根据商品id集合查询订阅里程包信息 出参：queryWrapper： "+ JSON.toJSONString(result));
        if(null != result && result.size() > 0) {
            return Convert.toGoodsProductSubscribeMileageDto(result).stream().collect(Collectors.groupingBy(GoodsSubscribeMileageDto::getGoodsId));
        }
        return null;
    }

    @Override
    public List<GoodsSubscribeMileageDto> getGoodsSubscribeMileageById(Integer goodsId) {
        log.info("根据商品id集合查询订阅里程包信息。入参 goodsId ： ["+ goodsId +"]");
        List<GoodsSubscribeMileageBean> result = goodsSubscribeMileageMapper.selectSubscribeMileageList(Arrays.asList(goodsId));
        log.info("根据商品id集合查询订阅里程包信息 出参：queryWrapper： "+ JSON.toJSONString(result));
        return Convert.toGoodsProductSubscribeMileageDto(result);
    }

    @Override
    public GoodsSubscribeMileageDto getSubscribeMileageById(Integer packageId) {
        log.info("根据里程包id集合查询订阅里程包信息。入参 packageId ： ["+ packageId +"]");
        GoodsSubscribeMileageBean result = goodsSubscribeMileageMapper.getSubscribeMileageById(packageId);
        log.info("根据商品id集合查询订阅里程包信息 出参：queryWrapper： "+ JSON.toJSONString(result));
        return Convert.toGoodsProductSubscribeMileageDto(result);
    }

    @Override
    public int createGoodsSubscribeMileage(int goodsId, List<GoodsSubscribePackageCreateDto> createDto) {
        log.info("插入订阅商品里程信息 createDto :"+JSON.toJSONString(createDto));
        for(GoodsSubscribePackageCreateDto goodsSubscribeMileageCreateDto : createDto) {
            if(goodsSubscribeMileageCreateDto.getPackageType() == GoodsMileageTypeEnum.SUBSCRIBE) {
                GoodsSubscribeMileageBean goodsProductSubscribeMileage = new GoodsSubscribeMileageBean();
                BeanUtils.copyProperties(goodsSubscribeMileageCreateDto, goodsProductSubscribeMileage);
                goodsProductSubscribeMileage.setGoodsId(goodsId);
                log.info("插入订阅商品里程信息 入参 goodsProductSpecBean :"+JSON.toJSONString(goodsProductSubscribeMileage));
                int result = goodsSubscribeMileageMapper.create(goodsProductSubscribeMileage);
                log.info("插入订阅商品里程信息 出参 result :"+JSON.toJSONString(result));
            }
        }
        return 1;
    }

}
